/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package acousticfield3d.gui.misc;

import acousticfield3d.gui.MainForm;
import acousticfield3d.math.M;
import acousticfield3d.math.Transform;
import acousticfield3d.math.Vector3f;
import acousticfield3d.scene.Entity;
import acousticfield3d.scene.MeshEntity;
import acousticfield3d.scene.Resources;
import acousticfield3d.shapes.Mesh;
import acousticfield3d.simulation.ControlPoint;
import acousticfield3d.simulation.Transducer;
import acousticfield3d.utils.Color;
import acousticfield3d.utils.FileUtils;
import acousticfield3d.utils.Parse;
import acousticfield3d.utils.TextFrame;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author am14010
 */
public class ScatterObjectForm extends javax.swing.JFrame {
    final MainForm mf;

    public ScatterObjectForm(MainForm mf) {
        this.mf = mf;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        objText = new javax.swing.JTextField();
        selectButton = new javax.swing.JButton();
        placeButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        scaleText = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        area = new javax.swing.JTextArea();
        cleanButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        placeScaleText = new javax.swing.JTextField();
        gen3DSText = new javax.swing.JButton();
        gen3DSText1 = new javax.swing.JButton();

        jButton2.setText("jButton2");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Obj:");

        selectButton.setText("Select");
        selectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectButtonActionPerformed(evt);
            }
        });

        placeButton.setText("Place");
        placeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                placeButtonActionPerformed(evt);
            }
        });

        jLabel2.setText("Scale:");

        scaleText.setText("0.0253936008");

        area.setColumns(20);
        area.setRows(5);
        jScrollPane1.setViewportView(area);

        cleanButton.setText("Clean");
        cleanButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cleanButtonActionPerformed(evt);
            }
        });

        jLabel3.setText("Scale:");

        placeScaleText.setText("1");

        gen3DSText.setText("Gen3DS");
        gen3DSText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gen3DSTextActionPerformed(evt);
            }
        });

        gen3DSText1.setText("Gen3DS from sim");
        gen3DSText1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gen3DSText1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(scaleText, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 218, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(objText)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(selectButton))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(cleanButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(gen3DSText1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(gen3DSText)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(placeScaleText, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(placeButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(objText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(selectButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(scaleText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(placeButton)
                    .addComponent(cleanButton)
                    .addComponent(jLabel3)
                    .addComponent(placeScaleText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(gen3DSText)
                    .addComponent(gen3DSText1))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void selectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectButtonActionPerformed
        final String file = FileUtils.selectFile(this, "open", ".obj", null);
        if ( file != null ){
            objText.setText(file);
        }
    }//GEN-LAST:event_selectButtonActionPerformed

    private void placeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_placeButtonActionPerformed
        try {
            final float scale = Parse.toFloat( scaleText.getText() );
            final float placeScale = Parse.toFloat( placeScaleText.getText() );
            //get the mesh
            final Mesh m = new Mesh();
            m.parse( FileUtils.getStringFromFile( new File(objText.getText())));
            
            //parse the positions and orientations
            final String[] lines = area.getText().split("\\n");
            for(String l : lines){
                final String[] pl = l.trim().split(" ");
                MeshEntity me = new MeshEntity(Resources.MESH_CUSTOM, null, Resources.SHADER_SOLID_SPEC);
                me.setTag( Entity.TAG_CONTROL_POINT | Entity.TAG_BEAD | Entity.TAG_OBJ);
                me.customMesh = m;
                mf.scene.getEntities().add( me );
               
                final Transform t = me.getTransform();
                t.getScale().set( scale );
                t.getTranslation().parse( pl[0] , pl[2] , pl[1] ).multLocal(placeScale);
                /*final Vector3f angles = new Vector3f(pl[3] , pl[5]  , pl[4]);
                t.getRotation().fromAngles(angles);*/
                t.lookAt(Vector3f.ZERO);
                t.rotateLocal(M.degToRad( -90 ), 0, 0);
            }
            
            mf.needUpdate();
        } catch (IOException ex) {
            Logger.getLogger(ScatterObjectForm.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }//GEN-LAST:event_placeButtonActionPerformed

    private void cleanButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cleanButtonActionPerformed
        mf.scene.removeWithTag( Entity.TAG_OBJ );
        mf.needUpdate();
    }//GEN-LAST:event_cleanButtonActionPerformed

    private void gen3DSTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gen3DSTextActionPerformed
        final StringBuilder sb = new StringBuilder();
        final String[] lines = area.getText().split("\\n");
        int index = 1;
        for (String l : lines) {
            final Entity e = new Entity();
            final String[] pl = l.trim().split(" ");
            final Vector3f pos = new Vector3f(pl[0], pl[2], pl[1]).multLocal(1, 1, 1);
            e.getTransform().getTranslation().set(pos);
            e.getTransform().lookAt(Vector3f.ZERO);
            e.getTransform().rotateLocal(M.degToRad(-90), 0, 0);
            
            /*final Vector3f angles = new Vector3f(pl[3] , pl[5]  , pl[4]);
             t.getRotation().fromAngles(angles);*/
            
            //selection[1].pos = [0, 0, 0]
            //rotate selection[1] (eulerangles 0 0 0)
            final Vector3f eulerAngles = new Vector3f();
            e.getTransform().getRotation().toAngles(eulerAngles);
            e.getTransform().getTranslation().multLocal( 1000.0f / 25.4f);
            pos.set(e.getTransform().getTranslation());
            
            sb.append("selection[" + index + "].pos = [" + pos.x + ", " + (-pos.z) + ", " + pos.y + "]\n");
            eulerAngles.multLocal( M.RAD_TO_DEG );
            sb.append("rotate selection[" + index + "] (eulerangles " + eulerAngles.x + " " + (-eulerAngles.z) + " " + eulerAngles.y + ")\n");
            index++;
        }
        
        TextFrame.showText("Script", sb.toString(), this);
    }//GEN-LAST:event_gen3DSTextActionPerformed

    private void gen3DSText1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gen3DSText1ActionPerformed
        final StringBuilder sb = new StringBuilder();
        
        int index = 1;
        final Vector3f pos = new Vector3f();
        for (Transducer t : mf.simulation.transducers) {
            
            final Vector3f eulerAngles = new Vector3f();
            t.getTransform().getRotation().toAngles(eulerAngles);
            pos.set( t.getTransform().getTranslation() );
            pos.multLocal( 1000.0f / 25.4f);
            
            sb.append("selection[" + index + "].pos = [" + pos.x + ", " + (-pos.z) + ", " + pos.y + "]\n");
            eulerAngles.multLocal( M.RAD_TO_DEG );
            sb.append("rotate selection[" + index + "] (eulerangles " + eulerAngles.x + " " + (-eulerAngles.z) + " " + eulerAngles.y + ")\n");
            index++;
        }
        
        TextFrame.showText("Script", sb.toString(), this);
    }//GEN-LAST:event_gen3DSText1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea area;
    private javax.swing.JButton cleanButton;
    private javax.swing.JButton gen3DSText;
    private javax.swing.JButton gen3DSText1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField objText;
    private javax.swing.JButton placeButton;
    private javax.swing.JTextField placeScaleText;
    private javax.swing.JTextField scaleText;
    private javax.swing.JButton selectButton;
    // End of variables declaration//GEN-END:variables
}
